<%
  button_class = local_assigns.fetch :button_class, "stripe-button-el"
  button_inner_style = local_assigns.fetch :button_inner_style, 'min-height: 30px'
  button_text = local_assigns.fetch :button_text, "Pay Now"
  product_class = sellable.product_class
  product_permalink = sellable.permalink
  price = local_assigns.fetch :price, sellable.price
  name = local_assigns.fetch :name, sellable.name
  description = local_assigns.fetch :description, "#{name} (#{formatted_price(price)})"
  product_image_path = local_assigns.fetch :product_image_path , ''
  panel_label = local_assigns.fetch :panel_label, ''
  allow_remember_me = local_assigns.fetch :allow_remember_me, true
  email = local_assigns.fetch :email, ''
  verify_zip_code = local_assigns.fetch :verify_zip_code, false
  custom_fields = local_assigns.fetch :custom_fields, nil
  billing_address = local_assigns.fetch :billing_address, false
  shipping_address = local_assigns.fetch :shipping_address, false
  bitcoin = local_assigns.fetch :bitcoin, false
  stripe_customer_id = local_assigns.fetch :stripe_customer_id, nil

  sale = Payola::Sale.new(product: sellable)

  button_id = "payola-button-#{product_class}-#{product_permalink}"

  form_id = "#{button_id}-form"

  currency = sellable.respond_to?(:currency) ? sellable.currency : Payola.default_currency

  error_div_id = local_assigns.fetch :error_div_id, ''
  if error_div_id.present?
    show_default_error_div = false
  else
    error_div_id = "#{button_id}-errors"
    show_default_error_div = true
  end

  raw_data = {
    base_path: main_app.payola_path,
    form_id: form_id,
    button_id: button_id,
    error_div_id: error_div_id,
    product_class: product_class,
    product_permalink: product_permalink,
    price: price,
    name: name,
    description: description,
    currency: currency,
    product_image_path: product_image_path,
    publishable_key: Payola.publishable_key_for_sale(sale),
    panel_label: panel_label,
    allow_remember_me: allow_remember_me,
    email: email,
    verify_zip_code: verify_zip_code,
    billing_address: billing_address,
    shipping_address: shipping_address,
    bitcoin: bitcoin,
    stripe_customer_id: stripe_customer_id
  }

  raw_data[:signed_custom_fields] = sale.verifier.generate(custom_fields) if custom_fields

  html_hash = {}
  raw_data.each do |k,v|
    html_hash["data-#{k}"] = v
  end
  html_hash["id"] = form_id
%>

<script src="https://checkout.stripe.com/checkout.js"></script>
<link rel="stylesheet" href="https://checkout.stripe.com/v3/checkout/button.css"></link>

<%= form_tag payola.buy_path(product_class: product_class, permalink: product_permalink), html_hash do %>
  <button class="<%= button_class %> payola-checkout-button" id="<%= button_id %>">
    <span class="payola-checkout-button-text" style="display: block; <%= button_inner_style %>"><%= button_text %></span>
    <span class="payola-checkout-button-spinner" style="display: none; <%= button_inner_style %>">Please wait...</span>
  </button>
  <% if show_default_error_div %>
    <div class="payola-error-target" style="display: none;" id="<%= error_div_id %>"></div>
  <% end %>
<% end %>
